package com.kelvin.paopao.module.main.date.datedetail;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.kelvin.kelvinlib.imageloader.GlideUtil;
import com.kelvin.paopao.R;
import com.kelvin.paopao.api.responseBean.DateListModel;
import com.kelvin.paopao.injector.components.DaggerDateDetailComponent;
import com.kelvin.paopao.injector.modules.DateDetailModul;
import com.kelvin.paopao.module.base.BaseActivity;
import com.kelvin.paopao.module.main.date.datemain.MainDateFragment;
import com.kelvin.paopao.module.person.photoset.PhotoSetActivity;
import com.kelvin.paopao.widgets.CustomToolbar;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by MCK on 2017/5/10.
 */

public class DateDetailActivity extends BaseActivity<IDateDetailPresenter> {
    @BindView(R.id.iv_avatar)
    ImageView ivAvatar;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.iv_gender)
    ImageView ivGender;
    @BindView(R.id.tv_age)
    TextView tvAge;
    @BindView(R.id.tv_distance)
    TextView tvDistance;
    @BindView(R.id.tv_time)
    TextView tvTime;
    @BindView(R.id.tv_date_title)
    TextView tvDateTitle;
    @BindView(R.id.tv_date_content1)
    TextView tvDateContent1;
    @BindView(R.id.tv_date_content2)
    TextView tvDateContent2;
    @BindView(R.id.tv_date_content3)
    TextView tvDateContent3;
    @BindView(R.id.iv_content1)
    ImageView ivContent1;
    @BindView(R.id.iv_content2)
    ImageView ivContent2;
    @BindView(R.id.iv_content3)
    ImageView ivContent3;
    @BindView(R.id.ll_image_content)
    LinearLayout llImageContent;
    @BindView(R.id.tv_date_time)
    TextView tvDateTime;
    @BindView(R.id.ll_time)
    LinearLayout llTime;
    @BindView(R.id.iv_type)
    ImageView ivType;
    @BindView(R.id.tv_date_location)
    TextView tvDateLocation;
    @BindView(R.id.custom_toolbar)
    CustomToolbar customToolbar;
    @BindView(R.id.tv_signup)
    TextView tvSignup;

    private DateListModel model;

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_date_detail;
    }

    @Override
    protected void initInjector() {
        DaggerDateDetailComponent.builder().dateDetailModul(new DateDetailModul(this))
                .build().inject(this);
    }

    private List<String> urls;

    @Override
    protected void initViews() {
        customToolbar.showBackIcon(this);
        model = (DateListModel) getIntent().getSerializableExtra("model");

        urls = new ArrayList<String>();
        urls.add(model.getImg1());
        urls.add(model.getImg2());
        urls.add(model.getImg3());

        GlideUtil.loadCommonImg(model.getUser_headimg(),
                R.drawable.default_img, ivAvatar, this);
        tvName.setText(model.getUser_name());

        int type = Integer.parseInt(model.getOrdertype());
        if (type == MainDateFragment.TYPE_DATE_OTHER) {
            ivType.setImageResource(R.drawable.icon_main_list_other_selected);
            tvDateTitle.setText("约人" + model.getItem_title());
        } else if (type == MainDateFragment.TYPE_DATE_SPORT) {
            ivType.setImageResource(R.drawable.icon_main_list_sport_selected);
            tvDateTitle.setText("约人运动" + model.getItem_title());
        } else if (type == MainDateFragment.TYPE_DATE_EAT) {
            ivType.setImageResource(R.drawable.icon_main_list_eat_selected);
            tvDateTitle.setText("约人吃饭" + model.getItem_title());
        } else if (type == MainDateFragment.TYPE_DATE_SING) {
            ivType.setImageResource(R.drawable.icon_main_list_sing_selected);
            tvDateTitle.setText("约人唱歌" + model.getItem_title());
        } else if (type == MainDateFragment.TYPE_DATE_MOVIES) {
            ivType.setImageResource(R.drawable.icon_main_list_movie_selected);
            tvDateTitle.setText("约人看电影" + model.getItem_title());
        } else if (type == MainDateFragment.TYPE_DATE_TRAVEL) {
            ivType.setImageResource(R.drawable.icon_main_list_travel_selected);
            tvDateTitle.setText("约人去旅游" + model.getItem_title());
        }


        if (TextUtils.isEmpty(model.getImg1()) && TextUtils.isEmpty(model.getImg2())
                && TextUtils.isEmpty(model.getImg3())) {
            llImageContent.setVisibility(View.GONE);
        } else {
            if (TextUtils.isEmpty(model.getImg1())) {
                ivContent1.setVisibility(View.GONE);
            }
            if (TextUtils.isEmpty(model.getImg2())) {
                ivContent2.setVisibility(View.GONE);
            }
            if (TextUtils.isEmpty(model.getImg3())) {
                ivContent3.setVisibility(View.GONE);
            }
            GlideUtil.loadCommonBG(model.getImg1(), R.drawable.default_img,
                    ivContent1, DateDetailActivity.this);
            GlideUtil.loadCommonBG(model.getImg2(), R.drawable.default_img,
                    ivContent2, DateDetailActivity.this);
            GlideUtil.loadCommonBG(model.getImg3(), R.drawable.default_img,
                    ivContent3, DateDetailActivity.this);
        }

        if (model.getOSex().equals("男")) {
            tvDateContent1.setText("只约会男生");
        } else {
            tvDateContent1.setText("只约会女生");
        }
        if (model.getIstip() == 0) {
            if (model.getOPayMode().equals("AA")) {
                tvDateContent2.setText("我们AA制");
            } else {
                tvDateContent2.setText("我买单");
            }
        } else {
            tvDateContent2.setText("有额外的酬劳(" + model.getTipPrice1() + "元/小时" + " *" + model.getHours1() + "小时)");
        }
        tvDateContent3.setText(model.getRemark());

        tvDateLocation.setText(model.getOAddress());

        if (!TextUtils.isEmpty(model.getODate())) {
            tvDateTime.setText(model.getODate());
        }
    }

    @Override
    protected void updateViews(boolean isRefresh) {
        mPresenter.isAlreadyJoin(model.getId());
    }

    public static void launch(Context context, DateListModel model) {
        Intent intent = new Intent(context, DateDetailActivity.class);
        intent.putExtra("model", model);
        context.startActivity(intent);
    }


    @OnClick({R.id.iv_content1, R.id.iv_content2, R.id.iv_content3, R.id.tv_signup})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_content1:
                PhotoSetActivity.launch(this, urls, 0);
                break;
            case R.id.iv_content2:
                PhotoSetActivity.launch(this, urls, 1);
                break;
            case R.id.iv_content3:
                PhotoSetActivity.launch(this, urls, 2);
                break;
            case R.id.tv_signup:
                if (tvSignup.getText().toString().equals("取消报名")) {
                    mPresenter.cancel(model.getId());
                } else {
                    mPresenter.signup(model.getId());
                }
                break;
        }
    }

    public void setAlreadyCheck() {
        tvSignup.setText("取消报名");
    }

}
